package cn.dliyy.warehouse.service;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.stereotype.Service;

import cn.dliyy.base.service.CommonService;
import cn.dliyy.base.service.ServiceException;
import cn.dliyy.warehouse.bean.BGoods;
import cn.dliyy.warehouse.bean.Category;

@Service
public class BGoodsService extends CommonService {

	public void save(BGoods goods) {

		/*
		 * BGoods tgoods = (BGoods)
		 * super.entityDao.createQuery("from bgoods a where a.specification=" +
		 * goods.getSpecification() + " and a.name=" + goods.getName() +
		 * " where a.categoryid=" + goods.getCategoryId()); if (tgoods != null)
		 * { throw new ServiceException();// 已经存在 }
		 */
		Category tcategory = (Category) super.get(Category.class, goods.getCategoryId());
		if (tcategory == null) {
			// 未创建分类，请先创建分类
		}
		if ("0".equals(tcategory.getHasgoods())) {
			tcategory.setHasgoods("1");
			super.update(tcategory);
		}
		super.save(goods);
	}

	public List<Object> getListByName(String name) {
	/*	try {
			name=java.net.URLDecoder.decode(name, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		String hql = "from BGoods where name like '" + name + "%' or specification like '" + name + "%'";
		List<Object> list = super.entityDao.createQuery(hql);
		return list;
	}
	 public BGoods get(String name,String specification) {
	     String hql = "from BGoods where name = '" + name + "' and specification = '" + specification + "'";
	     List<Object> list = super.entityDao.createQuery(hql);
	     if(list!=null&&list.size()>0){
	       return (BGoods)list.get(0);
	     }
	     return null;
	   }
	
}
